<!--
 * @Description: 修改密码弹窗
 * @Author: chenju
 * @Date: 2023-03-07 10:37:36
 * @LastEditors: qingyang
 * @LastEditTime: 2023-03-07 11:36:12
-->
<template>
  <div>
    <change-psw v-model:visible="showChangePsw"></change-psw>
  </div>
</template>
<script lang="ts">
import { computed, defineComponent, ref, watch } from 'vue';
import { store } from '@/store';
import { useMessage } from '@/hooks/useMessage';
import ChangePsw from '@/commonModule/layout/components/ChangePsw/index.vue';

export default defineComponent({
  name: 'PswDialog',
  components: { ChangePsw },
  setup() {
    const { createConfirm } = useMessage();
    const showChangePsw = ref(false);
    const dialogVisible = ref(false);
    const userInfo = computed(() => {
      return store.state.user.userInfo;
    });
    watch(
      () => userInfo.value,
      value => {
        const userType = store.state.user?.userType;
        // 仅账号密码登录才需要校验密码
        if (userType === 'CARBON') {
          value.realName && checkPwdFlag();
        }
      }
    );

    const checkPwdFlag = () => {
      // 检查密码是否过期
      let flag = userInfo.value.pwdFlag;
      if (!flag) {
        // pwdModifyTime 存在则表示不是首次登录
        const pwdModifyTime = userInfo.value.pwdModifyTime;
        createConfirm(
          pwdModifyTime ? '提示' : '首次登录',
          pwdModifyTime ? '密码已过期，需要重新修改密码！' : '为保证安全，请重置密码！',
          {
            iconType: 'warning',
            closeOnPressEscape: false,
            showCancelButton: false,
            showClose: false,
            autofocus: false
          }
        ).then(() => {
          showChangePsw.value = true;
        });
      }
    };

    return {
      dialogVisible,
      showChangePsw
    };
  }
});
</script>
